package com.xxx.yuedong.mall.controller;

import com.github.pagehelper.PageInfo;
import com.xxx.yuedong.common.R;
import com.xxx.yuedong.mall.controller.form.*;
import com.xxx.yuedong.mall.model.pojo.Product;
import com.xxx.yuedong.mall.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 描述：     后台商品管理Controller
 * @author hc
 */
@RestController
@Api(tags = "后台商品管理相关接口")
@Slf4j
public class ProductAdminController {

    @Autowired
    ProductService productService;

    @PostMapping("/admin/product/add")
    @ApiOperation(value = "后台添加商品")
    public R addProduct(@Valid @RequestBody AddProductForm form) {
        productService.add(form);
        return R.ok();
    }

//    @PostMapping("/admin/upload/file")
//    @ApiOperation(value = "图片上传")
//    public R upload(HttpServletRequest httpServletRequest,
//            @RequestParam("file") MultipartFile file) {
//        String fileName = file.getOriginalFilename();
//        String suffixName = fileName.substring(fileName.lastIndexOf("."));
//        //生成文件名称UUID
//        UUID uuid = UUID.randomUUID();
//        String newFileName = uuid.toString() + suffixName;
//        //创建文件
//        File fileDirectory = new File(Constant.FILE_UPLOAD_DIR);
//        File destFile = new File(Constant.FILE_UPLOAD_DIR + newFileName);
//        if (!fileDirectory.exists()) {
//            if (!fileDirectory.mkdir()) {
//                throw new MallException(MallExceptionEnum.MKDIR_FAILED);
//            }
//        }
//        try {
//            file.transferTo(destFile);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        try {
//            return R
//                    .ok(getHost(new URI(httpServletRequest.getRequestURL() + "")) + "/images/"
//                            + newFileName);
//        } catch (URISyntaxException e) {
//            return R.error();
//        }
//    }

//    private URI getHost(URI uri) {
//        URI effectiveURI;
//        try {
//            effectiveURI = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(),
//                    null, null, null);
//        } catch (URISyntaxException e) {
//            effectiveURI = null;
//        }
//        return effectiveURI;
//    }

    @ApiOperation(value = "后台更新商品")
    @PostMapping("/admin/product/update")
    public R updateProduct(@Valid @RequestBody UpdateProductForm form) {
        Product product = new Product();
        BeanUtils.copyProperties(form, product);
        productService.update(product);
        return R.ok();
    }

    @ApiOperation(value = "后台删除商品")
    @PostMapping("/admin/product/delete")
    public R deleteProduct(@Valid @RequestBody DeleteProductForm form) {
        productService.delete(form.getIds());
        return R.ok();
    }

    @ApiOperation(value = "后台批量上下架接口")
    @PostMapping("/admin/product/batchUpdateSellStatus")
    public R batchUpdateSellStatus(@Valid @RequestBody BatchUpdateSellStatusForm form) {
        productService.batchUpdateSellStatus(form.getIds(), form.getSellStatus());
        return R.ok();
    }

    @ApiOperation(value = "后台商品列表接口")
    @PostMapping("/admin/product/list")
    public R list(@Valid @RequestBody SearchProductListForm form) {
        PageInfo pageInfo = productService.listForAdmin(form.getPageNum(), form.getPageSize(),form.getName());
        return R.ok().put("page",pageInfo);
    }
}
